<?php
/**
 * 
 * Manages locale strings for all Solar classes.
 * 
 * @category Solar
 * 
 * @package Solar
 * 
 * @author Paul M. Jones <pmjones@solarphp.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id: Locale.php 2449 2007-04-22 03:00:12Z pmjones $
 * 
 */

/**
 * 
 * Manages locale strings for all Solar classes.
 * 
 * @category Solar
 * 
 * @package Solar
 * 
 */
class Abovo_Locale extends Solar_Locale {

    /**
     * 
     * Returns the translated locale string for a class and key.
     * 
     * Loads translations as needed.
     * 
     * @param string|object $spec The class name (or object) for the translation.
     * 
     * @param string $key The translation key.
     * 
     * @param mixed $num Helps determine whether to get a singular
     * or plural translation.
     * 
     * @return string A translated locale string.
     * 
     * @see Solar_Base::locale()
     * 
     * @see Manual::Solar/Using_locales
     * 
     */
    public function fetch($spec, $key, $num = 1, $replace = null)
    {
        // is the spec an object?
        if (is_object($spec)) {
            // yes, find its class
            $class = get_class($spec);
        } else {
            // no, assume the spec is a class name
            $class = (string) $spec;
        }
        
        // does the translation key exist for this class?
        // pre-empts the stack check.
        $string = $this->_trans($class, $key, $num, $replace);
        if ($string !== null) {
            return $string;
        }
        
        // find all parents of the class, including the class itself
        $parents = Solar::parents($class, true);
        
        // add the vendor namespace to the stack for vendor-wide strings,
        // and add Solar as the final fallback.
        $pos = strpos($class, '_');
        if ($pos !== false) {
            $vendor = substr($class, 0, $pos);
            $parents[] = $vendor;
            if ($vendor != 'Solar') {
                $parents[] = 'Solar';
            }
        } else {
            $parents[] = 'Solar';
        }
        
        $parents[] = 'Abovo';
        
        // go through all parents and find the first matching key
        foreach ($parents as $parent) {
            
            // do we need to load locale strings for the class?
            if (! array_key_exists($parent, $this->trans)) {
                $this->_load($parent);
            }
        
            // does the key exist for the parent?
            $string = $this->_trans($parent, $key, $num);
            if ($string !== null) {
                // save it for the class so we don't need to go through the
                // stack again, and then we're done.
                $this->trans[$class][$key] = $this->trans[$parent][$key];
                return $string;
            }
        }
        
        // never found a translation, return the requested key.
        return $key;
    }
    
   /**
    * 
    * Returns ISO 3166 country code for current locale code.
    * 
    * This is basicly just the last two uppercase letters
    * from the locale code.
    * 
    * @return string
    * 
    */
    public function getCountryCode()
    {
       return substr($this->_code, -2);
    }
}
